# -*- encoding: utf-8 -*-
import os
import sys
import time
from upload import Upload
reload(sys)
sys.setdefaultencoding('utf8')
sys.path.append('C:\project\py_project\mudiao')
'''
构造数据
1.查询文件夹，和文件，读取文件和图片

2.然后上传图片，把图片地址写到另一个目录的txt里面

3.然后操作云端数据库，把txt里面的值对应写到数据库

4.传完以后，再操作数据库，新上传的商品要显示到对应的面板里面

5.这个程序会经常用到，然后其它的单一更新直接用navicat解决

'''

class Product():
    '''python类里面的属性要这么弄才行'''
    def __init__(self):
        self.catalog = ""
        self.title = ""
        self.keyword = ""
        self.caizhi = ""
        self.chicun = ""
        self.jianjie = ""
        self.jieshao = ""
        self.image_suo = ""
        self.images = []

class File():

    def __init__(self):
        self.text = ""
        self.image = ""
        self.image_suo = ""

'''读取文件夹下的所有子文件夹，和子文件夹里面的文件'''
def readFolder(path):
    files = []
    dirs = os.listdir(path)
    '''获取一级目录'''
    for dir in dirs:
        dir  = path + "\\" + dir
        child_dirs = os.listdir(dir)
        file = File()

        '''以及目录下的子目录'''
        for child_dir in child_dirs:
            if child_dir == "text.txt":
                file.text = dir + "\\" +child_dir
            elif child_dir == "image":
                file.image = dir + "\\" +child_dir
            elif child_dir == "image-suo":
                file.image_suo = dir + "\\" + child_dir
        files.append(file)
    return files

'''
把txt文件的内容对应到product
把image路径对应到product里面images
'''
def set_product(files):
    products = []
    for file in files:
        product = Product()
        imgs = os.listdir(file.image)

        for image in imgs:
            image = file.image + "\\" + image.decode("gbk")
            product.images.append(image)

        imgs_suo = os.listdir(file.image_suo)

        for img_suo in imgs_suo:
            img_suo = file.image_suo + "\\" + img_suo.decode("gbk")
            product.image_suo = img_suo


        with open(file.text, "r") as f:
            text = f.readlines()

            for line in text:
                if line != "\n":
                    line = line.decode("gbk").replace("\n", "")
                    if "所属分类" in line:
                        line = line.split("----")
                        product.catalog = line[1]
                    if "标题" in line:
                        line = line.split("----")
                        product.title = line[1]

                    if "关键词" in line:
                        line = line.split("----")
                        product.keyword = line[1]

                    if "材质" in line:
                        line = line.split("----")
                        product.caizhi = line[1]

                    if "尺寸" in line:
                        line = line.split("----")
                        product.chicun = line[1]

                    if "简介" in line:
                        line = line.split("----")
                        product.jianjie = line[1]

                    if "介绍" in line:
                        line = line.split("----")
                        product.jieshao = line[1]

        products.append(product)

    return products

'''上传完图片以后保存到另一个文件里面'''
def write_result(products):

    for product in products:
        with open("temp/result.txt", "a") as f:
            f.write("所属分类----"+product.catalog+"\n")
            f.write("标题----" + product.title + "\n")
            f.write("关键词----" + product.keyword + "\n")
            f.write("材质----" + product.caizhi + "\n")
            f.write("尺寸----" + product.chicun + "\n")
            f.write("简介----" + product.jianjie + "\n")
            f.write("介绍----" + product.jieshao + "\n")
            f.write("缩略图----" + product.image_suo + "\n")
            image_path = ""
            for image in product.images:
                # 图片链接之间用8个横杠分割
                image_path = image_path + image + "________"
            f.write("图片----"+image_path[:-8] + "\n")
            f.write("------------------------------------分隔符------------------------------------\n")


if __name__=="__main__":
    upload = Upload("oss-cn-shenzhen.aliyuncs.com", "LTAIFAzR9vBQ7tzV", "N3Qex2GKP42hHHk4hrVuh3dk7E3Re3", "11410253")
    files = readFolder("C:\Users\Administrator\Desktop\mudiao_1")
    products = set_product(files)
    print len(products)
    for product in products:
        print product.catalog
        image_url = upload.upload(image_path=product.image_suo, file_name="tang/" + product.catalog + "_suo.png")
        product.image_suo = image_url
        for i in range(0, len(product.images)):
            image_path = product.images[i]
            image_url = upload.upload(image_path, "tang/" + product.catalog + str(++i) + ".png")
            product.images[i] = image_url

    files = write_result(products)
